import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import useUserStoreHook from "@/store/modules/user";
import path from "path";

//动态路由
// export const asyncRoutes = [approval, attendance, department, employee, permission, role, salary, social]

// import.meta.glob 为 vite 提供的特殊导入方式
// 它可以将模块中全部内容导入并返回一个Record对象
// 默认为懒加载模式 加入配置项 eager 将模块导入为同步模式
// const modules: Record<string, any> = import.meta.glob(['./modules/*.ts'], {
//   // 设置为 true 后，会将模块导入为同步模式
//   eager: true
// })

// 注册静态路由数组
const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "LoginPage",
    component: () => import("@/views/login/index.vue"),
    meta: {
      role: ["common", "admin"],
    },
  } as RouteRecordRaw,
  { path: "/", redirect: "/main" } as RouteRecordRaw,
  // 只有一级路由
  // 一级路由：负责把layout架子渲染出来
  // 二级路由：path为空 会作为默认的二级路由一上来就渲染出来
  {
    path: "/main",
    name: "main",
    meta: { title: "首页", icon: "dashboard" },
    component: () => import("@/views/main/main.vue"),
    children: [
      {
        path: "/main/dashboard",
        name: "dashboard",
        component: () => import("@/views/main/dashboard/index.vue"),
        meta: { title: "首页", icon: "dashboard" },
      },
      {
        path: "/main/approval",
        name: "approval",
        component: () => import("@/views/main/approval/index.vue"),
        meta: {
          title: "审批",
          icon: "tree-table",
        },
      },
      {
        path: "/main/department",
        name: "department",
        component: () => import("@/views/main/department/index.vue"),
        meta: {
          title: "组织",
          icon: "tree-table",
        },
      },
      {
        path: "/main/role",
        name: "role",
        component: () => import("@/views/main/role/index.vue"),
        meta: {
          title: "角色",
          icon: "tree-table",
        },
      },
      {
        path: "/main/employee",
        name: "employee",
        component: () => import("@/views/main/employee/index.vue"),
        meta: {
          title: "员工",
          icon: "people",
        },
      },
      {
        path: "/main/employee/detail/:id?",
        name: "employeeDetail",
        component: () => import("@/views/main/employee/detail.vue"),
        hidden: true, //隐藏在左侧菜单
        meta: {
          title: "员工详情", // 显示在导航的文本
          icon: "people",
        },
      },
      {
        path: "/main/permission",
        name: "permission",
        component: () => import("@/views/main/permission/index.vue"),
        meta: {
          title: "权限", // 显示在导航的文本
          icon: "people",
        },
      },
    ],
  } as RouteRecordRaw,
  { path: "/:pathMatch(.*)", component: () => import("@/views/404/index.vue") } as RouteRecordRaw,
];

// 将路由全部导入数组
// Object.keys(modules).forEach((key) => {
//   routes.push(modules[key].default)
// })
// 这样我们就可以在 module 中直接创建路由，无需再次在 index.ts 中手动引入了。

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...constantRoutes],
});

//----
// router.beforeEach((to) => {
//   nprogress.start();
//   const userStore = useUserStoreHook();
//   // 有token情况下
//   if (userStore.token) {
//     // 判断是否去登录页 去登录则 跳转至首页
//     if (to.path === "/login") {
//       next("/");
//     } else {
//       //判断是否有获取用户信息
//       if (!userStore.userInfo.userId) {
//         // 没有获取过用户信息 则调用接口获取用户信息 并保存到pinia中
//         userStore.getUserDataApiAction().then((res) => {
//           // 过滤路由
//           console.log(res);
//           // 将过滤后的路由保存在pinia中

//           //动态添加过滤后的路由数组

//           // 放行通过
//           next(to.path);
//         });
//       }
//     }
//   } else {
//     // 没有token 去登录页
//     // 判断是否在白名单
//     // if(){
//     //   // 在白名单 放行通过
//     // }else{
//     //   // 不在白名单 重定向到登录页
//     //   next("/login");
//     // }
//   }
// });
// 路由后置守卫
router.afterEach(() => {
  nprogress.done();
});

export default router;
